package org.fas.utils
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import org.fas.objectdata.DisplayerData;
	
	public class FuDisplayer
	{
		public static function distill(_container:DisplayObjectContainer,_instanceName:String):DisplayObject{
			var _target:DisplayObject = _container.getChildByName(_instanceName);
			if(_target==null){
				trace(_instanceName,'not in',_container);
				return null;
			}
			return _target;
		}
		public static function lvTop(_disOb:DisplayObject):void{
			var _par:Object = _disOb.parent;
			_par.setChildIndex(_disOb,_par.numChildren-1);
		}
		public static function removeAllIn(_ob:DisplayObjectContainer):void{
			while(_ob.numChildren) _ob.removeChildAt(0);
		}
		public static function removeSelf(_mc:*):void{
			var _par:DisplayObjectContainer = _mc.parent as DisplayObjectContainer;
			if(_par){
				_par.removeChild(_mc);
			}else{
				trace("ALARM in FuDisplayer.removeSelf: parent is null for",_mc);
			}
		}
		//customer hit test function
		/**
		 * check rim is hit with a real
		 * @param _ob
		 * @param _arr
		 * @return 
		 * 
		 */		
		 public static function checkRimSelectedReal(_ob:DisplayObject,_rim:DisplayObject):Boolean{
        	var i:int=0;
        	var j:int=0;
        	if(!_ob.hitTestObject(_rim)){
        		return false;
        	}
        	if(!checkRimSelectedRimHit(_ob,_rim)){
        		return true;
        	}else{
        		for(i=int(_rim.x);i<int(_rim.x+_rim.width);i++){
        			if(_ob.hitTestPoint(i,_rim.y,true)){
        				return true;
        			}
        		}
        		for(i=int(_rim.x);i<int(_rim.x+_rim.width);i++){
        			if(_ob.hitTestPoint(i,_rim.y+_rim.height,true)){
        				return true;
        			}
        		}
        		for(j=int(_rim.y);j<int(_rim.y+_rim.height);j++){
        			if(_ob.hitTestPoint(_rim.x,j,true)){
        				return true;
        			}
        		}
        		for(j=int(_rim.y);j<int(_rim.y+_rim.height);j++){
        			if(_ob.hitTestPoint(_rim.x+_rim.width,j,true)){
        				return true;
        			}
        		}
        	}
        	return false;
        }
        public static function checkRimSelectedRimHit(_ob:DisplayObject,_rim:DisplayObject):Boolean{
        	var _rect:Rectangle = _ob.getRect(_ob.parent as DisplayObjectContainer);
        	if(_rect.x<_rim.x){
        		return true;
        	}
        	if(_rect.y<_rim.y){
        		return true;
        	}
        	if((_rect.x+_rect.width)>(_rim.x+_rim.width)){
        		return true;
        	}
        	if((_rect.y+_rect.height)>(_rim.y+_rim.height)){
        		return true;
        	}
        	return false;
        }
		public static function hitTestArr(_ob:DisplayObject,_arr:Array,_shapeFlag:Boolean=true):Boolean{
        	var _is:Boolean = false;
        	var i:uint = 0;
        	var _point:Point;
        	while(i<_arr.length){
        		_point = _arr[i];
        		_is = _ob.hitTestPoint(_point.x,_point.y,_shapeFlag);
        		if(_is){
        			return _is;
        		}
        		i++
        	}
        	return _is;
        }
		public static function posParToChild(_child:DisplayObject,_x:Number,_y:Number):DisplayerData{
			var _pos:DisplayerData = new DisplayerData();
			var _w:Number = _x-_child.x;
			var _h:Number = _y-_child.y;
			var _roNow:Number = FuMath.angle(_w,_h);
			var _roRela:Number = (_roNow - _child.rotation);
			var _dis:Number = FuMath.dis(_x-_child.x,_y-_child.y);
			_pos.x = _dis*FuMath.cos(_roRela)/_child.scaleX;
			_pos.y = _dis*FuMath.sin(_roRela)/_child.scaleY;
			_pos.ro = _roRela;
			return _pos;
		}
		public static function posChildToPar(_child:DisplayObject,_x:Number,_y:Number):DisplayerData{
			var _pos:DisplayerData = new DisplayerData();
			var _roRela:Number = FuMath.angle(_x,_y);
			var _roNow:Number = _roRela + _child.rotation;
			var _dis:Number = FuMath.dis(_x,_y);
			_pos.x = _dis*FuMath.cos(_roNow)*_child.scaleX+_child.x;
			_pos.y = _dis*FuMath.sin(_roNow)*_child.scaleY+_child.y;
			_pos.ro = _roNow;
			_pos.scale = _child.scaleX;
			return _pos;
		}
	}
}